import { RouteRecordRaw, createRouter, createWebHashHistory } from "vue-router";
import Layout from "@/components/Layout/index.vue"

const routes: Array<RouteRecordRaw> = [
    {
        path: '/login',
        name: 'Login',
        component: () => import("@/views/Login/index.vue"),
        meta:{
            title:"登录页面",
            hidden: true
        }

    },
    {
        path: "/404",
        name:'NotFund',
        component:()=>import('@/views/NotFund/index.vue'),
        meta:{
            title:"404",
            hidden: true
        }
    },
    {
        path:'/',
        name:'Home',
        component:Layout,
        meta:{
            title:"首页",
            activeMenu:'/'
        },
        redirect:'/home',
        children:[
            {
                path:'/home',
                name:'Home',
                component:()=>import('@/views/Home/index.vue')
            }
        ]

    },
    {
        path:'/auth',
        name:'auth',
        component:Layout,
        meta:{
            title:"权限管理"
        },
        children:[
            {
                path:'/auth/user',
                name:'User',
                component:()=>import('@/views/UserAuth/index.vue'),
                meta:{
                    title:"用户管理"
                },
            },
            {
                path:'/auth/role',
                name:'Role',
                component:()=>import('@/views/Role/index.vue'),
                meta:{
                    title:"角色管理1"
                },
            },
            {
                path:'/auth/test',
                name:'Test',
                component:()=>import('@/views/test/index.vue'),
                meta:{
                    title:"测试"
                },
            },
            {
                path:'/auth/upload',
                name:'Upload',
                component:()=>import('@/views/upload/index.vue'),
                meta:{
                    title:"上传文件"
                },
            },
            
        ]

    },
    {
        path:'/goods',
        name:'Goods',
        component:Layout,
        meta:{
            title:"商品管理"
        },
        children:[
            {
                path:'/goods/index',
                name:'GoodsIndex',
                component:()=>import('@/views/Goods/index.vue'),
                meta:{
                    title:"商品信息管理"
                },
            },
            {
                path:'/goods/order',
                name:'Order',
                component:()=>import('@/views/Order/index.vue'),
                meta:{
                    title:"订单管理"
                },
            }
        ]

    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})
const whileList = ['/login']
import {setRoutes} from '@/utils/auth'

router.beforeEach((to,form,next)=>{
    let token = localStorage.getItem('token')
    //白名单 有值 或者登陆过存储了token信息可以跳转 否则就去登录页面
    if (whileList.includes(to.path) || token) {
        setRoutes()
        next()
    } else {
        next({path:'/login'})
    }
})
router.afterEach((to,from)=>{
    
})
export default router